<?php

// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// +----------------------------------------------------------------------


/**
 * 读取JSON文件,文件读写
 * @param $param
 * @return array
 */
function readjsonfile($jsonfile)
{
    if(file_exists($jsonfile))
    {
        $json_string = file_get_contents($jsonfile);  
        $data = json_decode($json_string, true);
        return $data;

    }
    else
    {
        return null;
    }

}


/**
 * 读取html文件,文件读写
 * @param $param
 * @return array
 */
function readhtmlfile($jsonfile)
{
    if(file_exists($jsonfile))
    {
        $json_string = file_get_contents($jsonfile);  
       
        return $json_string;

    }
    else
    {
        return "";
    }

}
/**
 * 写入HTML,文件读写
 * @param $param
 * @return array
 */
function writehtmlfile($jsonfile,$content)
{
$myfile = fopen($jsonfile, "w+") or die("Unable to open file!");
fwrite($myfile,$content);
fclose($myfile);
}



/**
 * 写入JSON文件,文件读写
 * @param $param
 * @return array
 */
function writejsonfile($jsonfile,$content)
{
$myfile = fopen($jsonfile, "w+") or die("Unable to open file!");
fwrite($myfile,$content);
fclose($myfile);
}


/**
 * 遍历文件
 * @param $param
 * @return array
 */
function myScanChildFile($dir)
{
    $dh  = opendir($dir);
    $tmp_arr = [];
    while (false !== ($filename = readdir($dh))) {
        if($filename !=".." && $filename !="."){
            if(is_dir($dir."/".$filename)){
               
            }else{
                $tmp_arr[] = $filename;
            }
        }

    }
    closedir($dh);
    return $tmp_arr;
}

/**
 * 遍历目录
 * @param $param
 * @return array
 */
function myScanChildDir($dir)
{
    $dh  = opendir($dir);
    $tmp_arr = [];
    while (false !== ($filename = readdir($dh))) {
        if($filename !=".." && $filename !="."){
            if(is_dir($dir."/".$filename)){
                $tmp_arr[] = $filename;
            }else{
              
            }
        }

    }
    closedir($dh);
    return $tmp_arr;
}


function myCreateDir($path)
{
    if (is_dir($path)){  
        return true;
    }else{
        $res=mkdir($path,0777,true); 
        if ($res){
            return true;
        }else{
            return false;
        }
    }
    return false;
}

/**
 * 遍历目录及文件
 * @param $param
 * @return array
 */
function myScanDirAndFile($dir)
{
    $dh  = opendir($dir);
    $tmp_arr = [];
    while (false !== ($filename = readdir($dh))) {
        if($filename !=".." && $filename !="."){
            if(is_dir($dir."/".$filename)){
                $tmp_arr[$filename] = myScanDirAndFile($dir."/".$filename);
            }else{
                $tmp_arr[] = $filename;
            }
        }

    }
    closedir($dh);
    return $tmp_arr;
}


/**
 * 数组操作函数
 * $arr1,$arr2键相同时更新第二个
 * @param $param
 * @return array
 */
function addarray($a1,$a2)
{
    if($a2==null){return $a1;}
    $result=array_merge($a1,$a2);
     return $result;
}





/**
 * 时间整数
 * @param $param
 * @return array
 */

function getintTime()
{
    return time();
}





/**
 * 菜单二级处理
 * @param $param
 * @return array
 */
function doListLeftMenu($param)
{
    $param =  json_decode(json_encode($param), true);
    $parent = []; $child = [];

    foreach($param as $key=>$vo){

        if(0 == $vo['parentid']){
            $vo['url'] = '#';
            $parent[] = $vo;
        }else{
            $vo['url'] = url($vo['controller'] .'/'. $vo['action']); 
            $child[] = $vo;
        }
    }

    foreach($parent as $key=>$vo){
        foreach($child as $k=>$v){
            if($v['parentid'] == $vo['id']){
                $parent[$key]['child'][] = $v;
            }
        }
    }
    unset($child);
    return $parent;
}


/**
 * 菜单二级处理
 * @param $param
 * @return array
 */
function doListMenu($param)
{
    $param =  json_decode(json_encode($param), true);
    $parent = []; $child = [];

    foreach($param as $key=>$vo){

        if(0 == $vo['parentid']){
           // $vo['url'] = '#';
            $parent[] = $vo;
        }else{
          //  $vo['url'] = url($vo['controller'] .'/'. $vo['action']); 
            $child[] = $vo;
        }
    }

    foreach($parent as $key=>$vo){
        foreach($child as $k=>$v){
            if($v['parentid'] == $vo['id']){
                $parent[$key]['child'][] = $v;
            }
        }
    }
    unset($child);


    $newparent = []; 
    foreach ($parent as $key=>$vo) {
        if (0 == $vo['parentid']) {
            $newparent[] = $vo;

            if(!empty($parent[$key]['child'])){
                foreach ($parent[$key]['child'] as $key2=>$vo2) {
                    $newparent[] = $vo2;
                }
    
            }
         
        }
    }


    return $newparent;
}



 function getWhereForm($param)
    {
        $where = [];
        foreach($param as $key=>$vo){
            if(strtolower($key)==strtolower("pageSize") ){continue;}
            if(strtolower($key)==strtolower("pageNumber") ){continue;}
            if($vo=="" ){continue;}

            $nkey=$key;
            $pos = strpos($key, '%'); if ($pos) { 
                 $nkey=substr($key,0,$pos); 
                 $where[$nkey]=array('like','%'.$vo.'%');
                 continue;
            }


            $pos = strpos($key, '<'); if ($pos) { 
                $nkey=substr($key,0,$pos); 
                $where[$nkey]=array('elt',$vo);
                continue;
           }

           $pos = strpos($key, '>'); if ($pos) { 
            $nkey=substr($key,0,$pos); 
            $where[$nkey]=array('egt',$vo);
            continue;
       }

         $where[$key]=$vo;
          
         }

         return $where;

    }
    